package org.dromara.business.service.impl;

import org.dromara.common.core.constant.UserConstants;
import org.dromara.common.core.utils.MapstructUtils;
import cn.hutool.core.util.StrUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.websocket.utils.WebSocketUtils;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.ReportBo;
import org.dromara.business.domain.vo.ReportVo;
import org.dromara.business.domain.Report;
import org.dromara.business.mapper.ReportMapper;
import org.dromara.business.service.ReportService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 反馈记录Service业务层处理
 *
 * @author zlj
 * @date 2024-08-20
 */
@RequiredArgsConstructor
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper,Report> implements ReportService {

    private final ReportMapper baseMapper;

    private final ScheduledExecutorService scheduledExecutorService;
    /**
     * 查询反馈记录
     */
    @Override
    public ReportVo queryById(Long reportId){
        return baseMapper.selectVoById(reportId);
    }

    /**
     * 查询反馈记录列表
     */
    @Override
    public TableDataInfo<ReportVo> queryPageList(ReportBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Report> lqw = buildQueryWrapper(bo);
        Page<ReportVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询反馈记录列表
     */
    @Override
    public List<ReportVo> queryList(ReportBo bo) {
        LambdaQueryWrapper<Report> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Report> buildQueryWrapper(ReportBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Report> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getUserName()),Report::getUserName,bo.getUserName());
        lqw.eq(bo.getUserId() != null, Report::getUserId, bo.getUserId());
        lqw.eq(bo.getReportType() != null, Report::getReportType, bo.getReportType());
        lqw.like(StrUtil.isNotBlank(bo.getReportText()), Report::getReportText, bo.getReportText());
        lqw.eq(bo.getStatus() != null, Report::getStatus, bo.getStatus());
        lqw.orderByDesc(Report::getCreateTime);
        return lqw;
    }

    /**
     * 新增反馈记录
     */
    @Override
    public Boolean insertByBo(ReportBo bo) {
        Report add = MapstructUtils.convert(bo, Report.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setReportId(add.getReportId());
        }

        scheduledExecutorService.schedule(() -> {
            WebSocketUtils.sendMessage(UserConstants.SUPER_ADMIN_ID, "您有一条新的反馈记录待处理");
        }, 2, TimeUnit.SECONDS);

        return flag;
    }

    /**
     * 修改反馈记录
     */
    @Override
    public Boolean updateByBo(ReportBo bo) {
        Report update = MapstructUtils.convert(bo, Report.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Report entity){

    }

    /**
     * 批量删除反馈记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
